const API_BASE_URL = 'http://192.168.2.90:7080' //正式环境
// const API_BASE_URL = 'http://192.168.2.16:10443' //局域网环境
// const API_BASE_URL = 'https://jnby.pinshizhineng.com:8084/mp/api'
const __baseURL = 'https://testbzhz.jnbygroup.com/gateway/api' //测试环境 外部扩展接口
// const __baseURL = 'https://bzhz.jnbygroup.com/gateway/api/' //正式环境
import * as ResponseStatus from '@/common/response-status.js'
import * as util from '@/common/util.js'
import * as api from '@/common/api.js'
import store from '@/store'
function httpRequest(method, url, data) {
  uni.showNavigationBarLoading();
  var that = this;
  return new Promise(function(resolve, reject) {
    uni.request({
      url: API_BASE_URL + url,
      data: data,
      method: method,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        resolve(res);
        uni.hideNavigationBarLoading();
      },
      fail: function(res) {
        reject(res);
        uni.hideNavigationBarLoading();
      },
    })
  });
}

function httpRequestJsonCustom(method, url, data) {
  uni.showNavigationBarLoading();
  var that = this;
  return new Promise(function(resolve, reject) {
    uni.request({
      url: __baseURL + url,
      data: data,
      method: method,
      header: {
        "Content-Type": "application/json",
      },
      success: function(res) {
        resolve(res);
        uni.hideNavigationBarLoading();
      },
      fail: function(res) {
        reject(res);
        uni.hideNavigationBarLoading();
      },
    })
  });
}

function httpRequestJson(method, url, data) {
  uni.showNavigationBarLoading();
  var that = this;
  return new Promise(function(resolve, reject) {
    uni.request({
      url: API_BASE_URL + url,
      data: data,
      method: method,
      header: {
        "Content-Type": "application/json",
		"token":uni.getStorageSync(util.USER_TOKEN_KEY)||""
      },
      success: function(res) {
		if(res.statusCode == ResponseStatus.OK){
			resolve(res);
			if(res.data.code != ResponseStatus.OK&&res.data.code != ResponseStatus.AUTHENTICATION_TOKEN_ERROR){
				util.showInfoToast(res.data.msg)
			}
			if(res.data.code == ResponseStatus.AUTHENTICATION_TOKEN_ERROR){
				util.refreshToken();
			}
		}else {
			reject(res);
			util.showInfoToast("请求出错")
		}
        uni.hideNavigationBarLoading();
      },
      fail: function(res) {
        reject(res);
        uni.hideNavigationBarLoading();
      },
    })
  });
}



function httpUploadRequest(url, path) {
  uni.showNavigationBarLoading();
  var that = this;
  return new Promise(function(resolve, reject) {
    uni.uploadFile({
      url: API_BASE_URL + url,
      filePath: path,
      name: 'imageFile',
      header: {
        "Content-Type": "multipart/form-data",
      },
      success: function(res) {
        resolve(res);
        uni.hideNavigationBarLoading();
      },
      fail: function(res) {
        reject(res);
        uni.hideNavigationBarLoading();
      },
    })

  });
}

function httpUploadMultRequest(url, data) {

  return new Promise(function(resolve, reject) {


  });

}

module.exports = {
  httpRequest: httpRequest,
  httpUploadRequest: httpUploadRequest,
  httpUploadMultRequest: httpUploadMultRequest,
  httpRequestJson: httpRequestJson,
  httpRequestJsonCustom:httpRequestJsonCustom
}